package cn.easybuy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.easybuy.utils.DataSourceUtil;

public class BaseDao {

	protected Connection conn;
	protected PreparedStatement ps;
	public BaseDao(Connection conn){
		this.conn=conn;
	}

	//通用增删改方法
	protected int executeUpdate(String sql,Object[]params) {
		int num=0;
		PreparedStatement ps=null;
		try {
			ps=conn.prepareStatement(sql);
			for(int i=0;i<params.length;i++){
				ps.setObject(i+1,params[i]);
			}
			num=ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DataSourceUtil.closeAll(conn, ps, null);
		}
		return num;
	}

	//通用查询方法
	protected ResultSet executeQuery(String sql,Object[]params) {
		ResultSet rs=null;
		try {
			ps=conn.prepareStatement(sql);
			if(params!=null){
				for(int i=0;i<params.length;i++){
					ps.setObject(i+1,params[i]);
				}
			}
			rs=ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}
}
